<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/metadata/productCategory/productCategoryList' }">商品分类列表</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="上级" prop="parentId">
                <autumn-tree-select :data="treeCategorys" v-model="editForm.parentId" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="editForm.sort" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { $convertTree } from 'src/util/tools'
import { listProductCategory, viewProductCategory, saveProductCategory, updateProductCategory } from 'src/api/metadata/productCategory'

const editFormRef = ref<FormInstance>()

//查询商品分类
const treeCategorys = ref<Array<any>>([])
listProductCategory().then(({ data }) => {
  treeCategorys.value = $convertTree(data)
})

// 定义表单数据
const editForm = reactive({
  id: '',
  parentId: '',
  code: '',
  name: '',
  sort: 1,
  remark: ''
})
const editFormRules = {
  code: [
    { required: true, message: '请输入商品分类编码', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入商品分类名称', trigger: 'blur' }
  ]
}

// 修改时填充表单数据
const route = useRoute()
nextTick(() => {
  if (route.query.id) {
    const id = route.query.id as string
    viewProductCategory(id).then(({ data }) => {
      Object.assign(editForm, data)
    })
  }
})

// 提交
const submit = function (formEl: FormInstance | undefined) {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveProductCategory(editForm).then(() => {
        cancel()
      })
    } else {
      updateProductCategory(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 关闭
const router = useRouter()
const cancel = function () {
  router.push('/metadata/productCategory/productCategoryList')
}

</script>

<style lang="scss" scoped>
.ui-fas {
  :deep(.el-input__inner) {
    border: var(--el-input-border, var(--el-border-base));
  }

  :deep(.el-input-group__prepend) {
    border: none;
  }
}
</style>
